package com.demo.jpademo.controller;

import com.demo.jpademo.pojo.MyUser;
import com.demo.jpademo.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/*
*myuser控制层CRUD
 */
@Controller
@RequestMapping("/myuser")
public class MyUserController {
//    注入myuser的service
    @Autowired
    private MyUserService myUserService;

//    查询所有
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ResponseBody
    public List<MyUser> findAll(){
        return this.myUserService.findAll();
    }

//    根据id查询
    @ResponseBody
    @RequestMapping(value = "findById" , method = RequestMethod.GET)
    public Optional<MyUser> findById(String id){
        return this.myUserService.findById(id);
    }
//    添加
    @PostMapping("/add")
    @ResponseBody
    public MyUser addMyUser(@RequestBody MyUser myUser){
        System.out.println(myUser.getUsername()+"---"+myUser.getPassword());
        return this.myUserService.addMyUser(myUser);
    }

//    根据ID删除
    @RequestMapping(value = "/del" ,method = RequestMethod.DELETE)
    @ResponseBody
    public int delById(String id){
        return this.myUserService.delById(id);
    }

//    根据ID更新
    @RequestMapping(value = "/up" , method = RequestMethod.PUT)
    @ResponseBody
    public MyUser upById(@RequestBody MyUser myUser){
        return this.myUserService.upById(myUser);
    }
}